﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using Jungle.Code.Utils;
using Jungle.Prodamo.AdvertManager;
using Jungle.Prodamo.Core;
using Jungle.Prodamo.MostNeedFunctions;

namespace Jungle.Prodamo.User.WebControls
{
    public class ByRubriks : WebControl
    {
        private int rubrikId;
        public int RubrikId { get { return rubrikId; } set { rubrikId = value; } }


        protected override void RenderContents(HtmlTextWriter writer)
        {
            ABR abr = new ABR();
            abr.RubrikId = rubrikId;
            abr.GetByRubrik();



            writer.AddAttribute("class", "advm_table");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            bool c = true;
            foreach (Advert adv in abr.AdvertList)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute("style", "border: 1px solid #7b9cbf;");
                writer.AddAttribute("class", "advm_td");
                if (adv.PremiumType) writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#aa0800");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                switch (adv.Type)
                {
                    case 0: writer.Write("Продажа"); break;
                    case 1: writer.Write("Покупка"); break;
                    case 2: writer.Write("Обмен"); break;
                }
                writer.RenderEndTag();

                writer.AddAttribute("style", "border: 1px solid #7b9cbf");
                writer.AddAttribute("class", "advm_td");
                if (adv.PremiumType) writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#aa0800");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(adv.Title);
                writer.RenderEndTag();

                writer.AddAttribute("style", "border: 1px solid #7b9cbf");
                writer.AddAttribute("class", "advm_td");
                if (adv.PremiumType) writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#aa0800");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(adv.Price);
                switch (adv.Currency)
                {
                    case 0: writer.Write(" грн."); break;
                    case 1: writer.Write(" евро"); break;
                    case 2: writer.Write(" $"); break;
                }
                writer.RenderEndTag();
                writer.RenderEndTag();

                if (c)
                {
                    writer.AddAttribute("style", "color: #F1F1F1;");
                    c = false;
                }
                else c = true;
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute("style", "border: 1px solid #7b9cbf;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                UserManager um = new UserManager();
                um.UserId = adv.UserId;
                um.GetById();
                if(um.UsertType) writer.Write(um.UserLink() + "<br/>");
                else writer.Write(um.CompanyLink() + "<br/>");
                writer.Write(adv.PubDate.ToString() + "<br/>");
                writer.Write(Functions.GetTimeLeft(adv.EndDate) + "<br/>");
                ABR abr2 = new ABR();
                abr2.AdvertId = adv.AdvertId;
                abr2.GetByAdvert();
                foreach (Rubrik rb in abr2.RubrikList)
                {
                    writer.Write(rb.Title + "&nbsp;");
                }
                writer.Write("<br/><a href=\"mailto:" + um.Email + "\">" + um.Email + "</a><br/>");
                if (um.Phone.Length > 0) writer.Write("тел: " + um.Phone + "<br/>");
                if (um.Faks.Length > 0) writer.Write("факс: " + um.Faks);
                writer.RenderEndTag();

                writer.AddAttribute("style", "border: 1px solid #7b9cbf;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(adv.Body + "<br/>");
                if (adv.ImageUrl.IndexOf("~/") != -1)
                {
                    writer.AddAttribute("style", "cursor: pointer; width: 100px; height: 100px;");
                    writer.AddAttribute("onclick", "OpenImg(this);");
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + adv.ImageUrl.Substring(1));
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                }
                writer.RenderEndTag();

                writer.AddAttribute("style", "border: 1px solid #7b9cbf;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("<a href=\"" + UrlHelper.GetSiteUrl() + "/ru/adverts.aspx?cmd=Show&Id=" + adv.AdvertId.ToString() + "\">Подробнее</a>");
                writer.RenderEndTag();
                writer.RenderEndTag();
            }

            base.RenderContents(writer);
        }
    }
}
